import {post} from "@/utils/http.js";
import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import useAccountStore from "@/store/modules/AccountStore.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";
import request from "@/utils/request.js";

export const addOrderAPI = (order) => {
    return post(
        '/order/add',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            ...order
        }
    ).then(
        res => {
            return res
        }
    )
}

export const payOrderAPI = (orderId, status) => {
    return post(
        '/order/pay',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, status
        }
    ).then(
        res => {
            return res
        }
    )
}

export const getAllOrderAPI = () => {
    const desKey = genDesKey()
    return request.get(
        '/order/all',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                userId: useAccountStore().user.id,
                desKey: rsaPubEncrypt(desKey),
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            // elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateOrderAPI = (orderId, status) => {
    return post(
        '/order/update',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, status
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const cancelOrderAPI = (orderId, status) => {
    return post(
        '/order/cancel',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, status
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const commentAPI = (orderId, productId, userId, content, score, img) => {
    return post(
        '/order/comment',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            orderId, productId, userId,
            content: rsaPubEncrypt(content),
            score, img
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}